package upicgame.gamelibrary.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import upicgame.entity.Game;
import upicgame.entity.Library;
import upicgame.entity.User;
import upicgame.game.service.GameService;
import upicgame.gamelibrary.service.LibraryService;
import upicgame.utils.Page;

@Controller
public class LibraryController {
	@Autowired
	private LibraryService ls;
	@Autowired
	private GameService gs;
	//用户使用
	//分页 根据用户ID查询库
	@RequestMapping("showLibrary")
	public String showLibrary(@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage,
			@RequestParam(value = "maxSize", defaultValue = "10", required = false) Integer maxSize, Model model) {
		if (uid != null) {
			Page<Library> page = ls.getLibraryByPage(uid, currentPage, maxSize);
			model.addAttribute("page", page);
			return "/userLibrary.jsp";
		} else {
			return "/index.jsp";
		}
	}
	//后台使用
	//根据用户ID查询库
	@RequestMapping("adminSingleFindLib")
	public String adminShowLibrary(@RequestParam(value = "uid", required = true) Integer uid, Model model) {
		if (uid != null) {
			List<Library> list = ls.getLibraryByUid(uid);
			model.addAttribute("list", list);
			return "/singleUserLibraryManager.jsp";
		} else {
			return "redirect:/showLibrary";
		}
	}
	//后台使用
	@RequestMapping("findAllLibraryByPage")
	public String findAllLibraryByPage(
			@RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage,
			@RequestParam(value = "maxSize", defaultValue = "10", required = false) Integer maxSize, Model model) {
		Page<Library> page = ls.findAllLibraryByPage(currentPage, maxSize);
		model.addAttribute("page", page);
		return "/userLibraryManager.jsp";
	}

	@RequestMapping("showAddMessage")
	public String showAddMessage(Model model) {
		List<Game> glist = gs.findAllGame();
		List<User> ulist = ls.findAllUser();
		model.addAttribute("ulist", ulist);
		model.addAttribute("glist", glist);
		return "/addUserLibrary.jsp";
	}
	
	@RequestMapping("addGameInLibrary")
	public String addGameInLibrary(@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "gid", required = true) Integer gid, Model model) {
		boolean result = ls.addGameInLibrary(uid, gid);
		if (!result) {
			model.addAttribute("msg", "添加失败");
		}
		return "redirect:/findAllLibraryByPage";
	}

 
	@RequestMapping("delGameInLibrary")
	public String delGameInLibrary(Integer uid, Integer gid, Integer currentPage, Model model) {
		boolean result = ls.delGameInLibrary(uid, gid);
		if(currentPage!=null) {
			if (!result) {
				model.addAttribute("msg", "删除失败");
			}
			return "redirect:/findAllLibraryByPage?currentPage=" + currentPage;
		}else {
			if (!result) {
				model.addAttribute("msg", "删除失败");
			}
			return "redirect:/adminSingleFindLib?uid=" + uid;
		}
	}
}
